<template>
  <div id="app">
    <!-- 登录页面 -->
    <Login v-if="showLogin" @login-success="handleLoginSuccess" />

    <!-- 主界面 -->
    <div v-else class="main-app">
      <header class="header">
        <div class="container">
          <h1 class="logo">💎 {{ systemName }}</h1>
          <nav class="nav">
          <button 
            :class="['nav-item', { active: currentView === 'dashboard' }]"
            @click="currentView = 'dashboard'"
          >
            数据统计
          </button>
          <button 
            :class="['nav-item', { active: currentView === 'users' }]"
            @click="currentView = 'users'"
          >
            用户管理
          </button>
          <button 
            :class="['nav-item', { active: currentView === 'categories' }]"
            @click="currentView = 'categories'"
          >
            分类管理
          </button>
          <button 
            :class="['nav-item', { active: currentView === 'products' }]"
            @click="currentView = 'products'"
          >
            商品管理
          </button>
          <button 
            :class="['nav-item', { active: currentView === 'orders' }]"
            @click="currentView = 'orders'"
          >
            订单管理
          </button>
          <button 
            :class="['nav-item', { active: currentView === 'reviews' }]"
            @click="currentView = 'reviews'"
          >
            评论管理
          </button>
          <button 
            :class="['nav-item', { active: currentView === 'config' }]"
            @click="currentView = 'config'"
          >
            系统配置
          </button>
        </nav>
        
        <!-- 用户信息和退出 -->
        <div class="user-section">
          <span class="admin-name">{{ adminInfo?.username }}</span>
          <button class="logout-btn" @click="handleLogout">退出</button>
        </div>
      </div>
    </header>

    <main class="main">
      <div class="container">
        <Dashboard v-if="currentView === 'dashboard'" />
        <UserManagement v-else-if="currentView === 'users'" />
        <CategoryManagement v-else-if="currentView === 'categories'" />
        <ProductList v-else-if="currentView === 'products'" />
        <OrderList v-else-if="currentView === 'orders'" />
        <ReviewManagement v-else-if="currentView === 'reviews'" />
        <SystemConfig v-else-if="currentView === 'config'" />
      </div>
    </main>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import Login from './views/Login.vue';
import Dashboard from './components/Dashboard.vue';
import UserManagement from './components/UserManagement.vue';
import CategoryManagement from './components/CategoryManagement.vue';
import OrderList from './components/OrderList.vue';
import ProductList from './components/ProductList.vue';
import ReviewManagement from './components/ReviewManagement.vue';
import SystemConfig from './components/SystemConfig.vue';
import { getConfig } from './api/config';
import { adminLogout } from './api/admin';

const currentView = ref('dashboard');
const showLogin = ref(false);
const systemName = ref('管理后台');
const systemVersion = ref('1.0.3');
const adminInfo = ref(null);

// 检查登录状态
const checkLogin = () => {
  const token = localStorage.getItem('adminToken');
  const info = localStorage.getItem('adminInfo');
  
  if (token && info) {
    try {
      adminInfo.value = JSON.parse(info);
      showLogin.value = false;
    } catch (e) {
      showLogin.value = true;
    }
  } else {
    showLogin.value = true;
  }
};

// 登录成功回调
const handleLoginSuccess = () => {
  checkLogin();
  loadSystemConfig();
};

// 退出登录
const handleLogout = async () => {
  if (!confirm('确定要退出登录吗？')) return;

  try {
    await adminLogout();
  } catch (error) {
    console.error('退出登录失败:', error);
  }

  localStorage.removeItem('adminToken');
  localStorage.removeItem('adminInfo');
  adminInfo.value = null;
  showLogin.value = true;
};

// 加载系统配置
const loadSystemConfig = async () => {
  try {
    const res = await getConfig();
    if (res.success) {
      systemName.value = res.data.system_name || '管理后台';
      systemVersion.value = res.data.system_version || '1.0.3';
      document.title = `${systemName.value} - 管理后台`;
    }
  } catch (error) {
    console.error('加载配置失败:', error);
  }
};

onMounted(() => {
  checkLogin();
  if (!showLogin.value) {
    loadSystemConfig();
  }
});
</script>

<style scoped>
#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  position: sticky;
  top: 0;
  z-index: 100;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
}

.logo {
  font-size: 20px;
  font-weight: 600;
  color: #667eea;
  margin: 0;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
}

.nav {
  display: flex;
  gap: 8px;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-left: auto;
}

.admin-name {
  font-size: 14px;
  color: #666;
  padding: 6px 12px;
  background: #f5f5f5;
  border-radius: 6px;
}

.logout-btn {
  padding: 8px 20px;
  background: transparent;
  border: 1px solid #e5e5e5;
  border-radius: 6px;
  color: #666;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.logout-btn:hover {
  border-color: #e23;
  color: #e23;
  background: #fff5f5;
}

.nav-item {
  padding: 8px 20px;
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  color: #666;
  transition: all 0.3s;
}

.nav-item:hover {
  background: #f5f5f5;
  color: #333;
}

.nav-item.active {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  color: #667eea;
  font-weight: 500;
}

.main {
  flex: 1;
  padding: 24px 0;
}
</style>


